package com.example.greedysnake.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelStore;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.greedysnake.MainActivity;
import com.example.greedysnake.R;
import com.example.greedysnake.datas.SnakeGamingData;
import com.example.greedysnake.enums.GamingLevel;

public class SnakeLevelActivity extends AppCompatActivity {

    private static final String TAG = SnakeLevelActivity.class.getSimpleName();

    RadioButton radioButtonSimpel, radioButtonNormal, radioButtonHard;
    RadioGroup radioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_snake_level);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        radioGroup = findViewById(R.id.snake_level_radio_btn_group);

//        radioButtonHard = findViewById(R.id.snake_level_hard);
//        radioButtonNormal = findViewById(R.id.snake_level_normal);
//        radioButtonSimpel = findViewById(R.id.snake_level_simple);

        switch (getLevel()){
            case HARD:{
                //radioButtonHard.setChecked(true);
                radioGroup.check(R.id.snake_level_hard);
            }break;
            case NORMAL:{
                //radioButtonNormal.setChecked(true);
                radioGroup.check(R.id.snake_level_normal);
            }break;
            case SIMPLE:{
                //radioButtonSimpel.setChecked(true);
                radioGroup.check(R.id.snake_level_simple);
            }break;
            default:
        }

    }

    public GamingLevel getLevel(){
        SharedPreferences sharedPreferences = getApplication().getSharedPreferences(SnakeGamingData.KEY_SHP_FILE_NAME, Context.MODE_PRIVATE);
        int level = sharedPreferences.getInt(SnakeGamingData.KEY_SHP_LEVEL, GamingLevel.NORMAL.getMoveSpeed());
        if(GamingLevel.HARD.getMoveSpeed() == level){
            return GamingLevel.HARD;
        }else if (GamingLevel.SIMPLE.getMoveSpeed() == level){
            return GamingLevel.SIMPLE;
        }
        return GamingLevel.NORMAL;
    }
    public void setLevel(GamingLevel gamingLevel){
        SharedPreferences sharedPreferences = getApplication().getSharedPreferences(SnakeGamingData.KEY_SHP_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putInt(SnakeGamingData.KEY_SHP_LEVEL, gamingLevel.getMoveSpeed());
        edit.apply();
    }

    public void save(View view){
        int checkedRadioButtonId = radioGroup.getCheckedRadioButtonId();
        switch (checkedRadioButtonId){
            case R.id.snake_level_hard: {
                setLevel(GamingLevel.HARD);
            }break;
            case R.id.snake_level_normal: {
                setLevel(GamingLevel.NORMAL);
            }break;
            case R.id.snake_level_simple: {
                setLevel(GamingLevel.SIMPLE);
            }break;
            default:
        }
        finish();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
}